package com.vr.xuecheng.base.util;

import org.springframework.util.FileCopyUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;

/**
 * @author hzh
 * @date 2025/03/22
 * multipart工具类
 */
public class MultipartFileUtil implements MultipartFile {
    private final String name;
    private final byte[] content;
    private final String contentType;

    private MultipartFileUtil(String name, byte[] content, String contentType) {
        this.name = name;
        this.content = content;
        this.contentType = contentType;
    }
    public static MultipartFileUtil build(String name, byte[] content, String contentType) {
        return new MultipartFileUtil(name, content, contentType);
    }

    @Override
    public String getName() { return name; }

    @Override
    public String getOriginalFilename() { return name; }

    @Override
    public String getContentType() { return contentType; }

    @Override
    public boolean isEmpty() { return content.length == 0; }

    @Override
    public long getSize() { return content.length; }

    @Override
    public byte[] getBytes() { return content; }

    @Override
    public InputStream getInputStream() {
        return new ByteArrayInputStream(content);
    }

    @Override
    public void transferTo(File dest) throws IOException {
        FileCopyUtils.copy(content, dest);
    }
}
